package cn.jwcat.controller;

import cn.jwcat.dealEntity.Bool;
import cn.jwcat.dealEntity.DealVolunteer;
import cn.jwcat.entity.*;
import cn.jwcat.service.*;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 志愿者操作
 */

@RestController
@Transactional
@RequestMapping("/volunteer")
public class VolunteerController {
    @Resource
    IVolunteerService volunteerService;

    @Resource
    IStaffService staffService;

    @Resource
    IStaffRoleService staffRoleService;

    @Resource
    IStaffAreaService staffAreaService;

    @Resource
    IHealthService healthService;

    // 志愿者查询操作
    @GetMapping("/{page}/{size}")
    @ApiOperation("分页查询")
    public Result getVolunteer(@PathVariable("page") int page, @PathVariable("size") int size) {
        PageHelper.startPage(page, size);
        List<Volunteer> volunteers = volunteerService.getVolunteer();
        PageInfo<Volunteer> volunteerPageInfo = new PageInfo<>(volunteers);
        return Result.success(volunteerPageInfo);
    }

    // 志愿者删除操作
    @DeleteMapping("/{id}")
    @ApiOperation("根基id删除")
    public Result deleteVolunteer(@PathVariable("id") int staffId) {
        boolean removeHealth = false;
        // 删除staff表的数据
        Staff staff = staffService.getById(staffId);
        boolean removeStaff = staffService.removeById(staffId);
        // 删除staffRole表的数据
        LambdaQueryWrapper<StaffRole> staffRoleWrapper = new LambdaQueryWrapper<>();
        staffRoleWrapper.eq(StaffRole::getStaffId, staffId);
        boolean removeStaffRole = staffRoleService.remove(staffRoleWrapper);
        // 删除staffArea表的数据
        LambdaQueryWrapper<StaffArea> staffAreaWrapper = new LambdaQueryWrapper<>();
        staffAreaWrapper.eq(StaffArea::getStaffId, staffId);
        boolean removeStaffArea = staffAreaService.remove(staffAreaWrapper);
        // 删除health表的数据
        LambdaQueryWrapper<Health> healthLambdaQueryWrapper = new LambdaQueryWrapper<>();
        healthLambdaQueryWrapper.eq(Health::getStaffId, staffId);
        List<Health> list = healthService.list(healthLambdaQueryWrapper);
        if (list.size() != 0) {
            removeHealth = healthService.remove(healthLambdaQueryWrapper);
        }
        String name = staff.getUsername();
        if (removeStaff && removeStaffRole && removeStaffArea && (list.size() == 0 || removeHealth))
            return Result.success(name + "的数据删除成功");
        return Result.error().message(name + "的数据删除失败");
    }

    // 志愿者状态修改操作
    @PutMapping("/status")
    @ApiOperation("修改状态")
    public Result changeStatus(@RequestBody Bool bool) {
        String name = bool.getName();
        LambdaUpdateWrapper<Staff> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Staff::getStatus, bool.getBool()).eq(Staff::getStaffId, bool.getId());
        boolean updateBool = staffService.update(updateWrapper);
        if (updateBool) return Result.success(name + "的账号状态修改成功");
        return Result.error(name + "的账号状态修改失败");
    }

    // 添加志愿者信息
    @PostMapping
    @ApiOperation("添加")
    public Result addVol(@RequestBody DealVolunteer dealVolunteer) {
        String username = dealVolunteer.getUsername();
        // staff对象操作
        Staff staff = new Staff();
        staff.setIdCard(dealVolunteer.getIdCard());
        staff.setUsername(username);
        staff.setTelephone(dealVolunteer.getTelephone());
        boolean saveStaff = staffService.save(staff);
        Integer staffId = staff.getStaffId();
        // staffRole对象操作
        StaffRole staffRole = new StaffRole();
        staffRole.setStaffId(staffId).setRoleId(2);
        boolean saveStaffRole = staffRoleService.save(staffRole);
        // staffArea对象操作
        StaffArea staffArea = new StaffArea();
        staffArea.setStaffId(staffId).setAreaId(dealVolunteer.getAreaId());
        boolean saveStaffArea = staffAreaService.save(staffArea);
        if (saveStaff && saveStaffRole && saveStaffArea) return Result.success(username + "添加成功");
        return Result.error(username + "添加失败");
    }

    // 志愿者的模糊查询
    @PostMapping("/query/{page}/{size}")
    @ApiOperation("模糊查询")
    public Result queryVol(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody DealVolunteer dealVolunteer) {
        PageHelper.startPage(page, size);
        List<Volunteer> volunteers = volunteerService.queryVol(dealVolunteer);
        PageInfo<Volunteer> volunteerPageInfo = new PageInfo<>(volunteers);
        return Result.success(volunteerPageInfo);
    }

    // 通过志愿者id获取数据
    @GetMapping("/{id}")
    @ApiOperation("根据id获取数据")
    public Result getVolunteer(@PathVariable("id") int id) {
        DealVolunteer dealVolunteer = volunteerService.getVolById(id);
        return Result.success(dealVolunteer);
    }

    // 修改志愿者的服务区域
    @PutMapping
    @ApiOperation("更新服务区域")
    public Result updateAreaId(@RequestBody DealVolunteer dealVolunteer) {
        LambdaUpdateWrapper<StaffArea> staffAreaWrapper = new LambdaUpdateWrapper<>();
        staffAreaWrapper.set(StaffArea::getAreaId, dealVolunteer.getAreaId()).eq(StaffArea::getStaffId, dealVolunteer.getStaffId());
        boolean bool = staffAreaService.update(staffAreaWrapper);
        if (bool) return Result.success(dealVolunteer.getUsername() + "的服务区域状态修改成功");
        return Result.error(dealVolunteer.getUsername() + "的服务区域修改完毕");
    }
}
